<template>
  <el-container>
    <el-header>
      <el-row class="visitandpiont">
        <el-col>
          <h1 class="carout">设备分类</h1>
        </el-col>
      </el-row>
    </el-header>
    <el-main>
      <div class="today" v-show="tab === 1">
        <el-row>
          <el-col>
            <el-button @click="dialogFormVisible = true" class="buttonb">
              <h3>新建分类</h3>
            </el-button>
          </el-col>
        </el-row>

        <el-row class="table">
          <el-table :data="tableData" style="width: 100%">
            <el-table-column prop="devTid" label="编号">
              <template slot-scope="scope">
                <span>{{ scope.$index + 1 }}</span>
              </template>
            </el-table-column>
            <el-table-column prop="name" label="分类"></el-table-column>
            <el-table-column prop="amount" label="数量"></el-table-column>
            <el-table-column prop="remark" label="备注"></el-table-column>
            <el-table-column prop="createTime" label="创建时间"></el-table-column>
            <el-table-column label="操作">
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  @click="
                    handleEdit(scope.$index, scope.row),
                      (dialogFormVisible1 = true)
                  "
                >编辑</el-button>
                <el-button
                  size="mini"
                  type="danger"
                  @click="handleDelete(scope.$index, scope.row)"
                >删除</el-button>
                <el-dialog :visible.sync="dialogFormVisible">
                  <el-form :model="form" class="dialog-body">
                    <el-form-item label="分类" :label-width="formLabelWidth">
                      <el-input v-model="form.name" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="数量" :label-width="formLabelWidth">
                      <el-input v-model="form.name1" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="备注" :label-width="formLabelWidth">
                      <el-input v-model="form.name2" autocomplete="off"></el-input>
                    </el-form-item>
                  </el-form>
                  <div slot="footer">
                    <el-button class="dialog-footer1" @click="dialogFormVisible = false">取 消</el-button>
                    <el-button class="dialog-footer2" type="primary" @click="add">确 定</el-button>
                  </div>
                </el-dialog>

                <el-dialog :visible.sync="dialogFormVisible1">
                  <el-form :model="form" class="dialog-body">
                    <el-form-item label="分类" :label-width="formLabelWidth">
                      <el-input v-model="form.name3" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="数量" :label-width="formLabelWidth">
                      <el-input v-model="form.name4" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="备注" :label-width="formLabelWidth">
                      <el-input v-model="form.name5" autocomplete="off"></el-input>
                    </el-form-item>
                  </el-form>
                  <div slot="footer">
                    <el-button class="dialog-footer1" @click="dialogFormVisible1 = false">取 消</el-button>
                    <el-button class="dialog-footer2" type="primary" @click="update_equitment">确 定</el-button>
                  </div>
                </el-dialog>
              </template>
            </el-table-column>
          </el-table>
        </el-row>
      </div>
    </el-main>
  </el-container>
</template>

<script>
export default {
  name: "",
  data() {
    return {
      formInline: {
        user: "",
        region: "",
      },
      value: "", //日期选择
      value1: "",

      tableData: [
        {
          devTid: '',
          name: '',
          amount: '',
          remark: '',
          createTime: '',
        },
      ],
      tab: 1,
      dialogFormVisible: false,
      dialogFormVisible1: false,
      form: {
        name: "",
        name1: "",
        name2: "",
        name3: "",
        name4: "",
        name5: "",
        region: "",
        date1: "",
        date2: "",
        delivery: false,
        type: [],
        resource: "",
        desc: "",
      },
      formLabelWidth: "80px",
    };
  },
  computed: {
    scId() {
      return this.$store.state.scId;
    },
  },
  methods: {
    getdate(date) {
      let now = date,
        y = now.getFullYear(),
        m = now.getMonth() + 1,
        d = now.getDate();
      return (
        y +
        "-" +
        (m < 10 ? "0" + m : m) +
        "-" +
        (d < 10 ? "0" + d : d) +
        " " +
        now.toTimeString().substr(0, 8)
      );
    },
    async getDeviceAll() {
      await this.$axios({
        methods: "get",
        url: `api/${this.scId}/deviceType/all`,
      })
        .then((res) => {
          let data = res.data.data;
          this.tableData = data;
          for (let i = 0; i < this.tableData.length; i++) {
            this.tableData[i].createTime = this.getdate(
              new Date(data[i].createTime)
            );
          }
          console.log(data);
        })
        .catch((err) => {
          this.$message.error("请求失败");
          console.log(err);
        });
    },
    async add() {
      this.$axios({
        url: `api/${this.scId}/deviceType/add`,
        method: "post",
        data: {
          name: this.form.name,
          amount: this.form.name1,
          remark: this.form.name2,
        },
        headers: {
          "Content-Type": "application/json",
        },
      }).then((res) => {
        this.getDeviceAll();
        console.log(res);
      });
      this.dialogFormVisible = false;
    },
    async delete_equitment(id) {
      this.$axios({
        url: `api/${this.scId}/deviceType/delete`,
        method: "delete",
        data: {
          devTid: id,
        },
        headers: {
          "Content-Type": "application/json",
        },
      }).then((res) => {
        this.getDeviceAll();
        console.log(res);
        console.log(this.tableData.splice());
      });
    },
    async update_equitment() {
      this.$axios({
        url: `api/${this.scId}/deviceType/saveById`,
        method: "post",
        data: {
          devTid: this.form.devTid,
          name: this.form.name3,
          amount: this.form.name4,
          remark: this.form.name5,
        },
        headers: {
          "Content-Type": "application/json",
        },
      }).then((res) => {
        this.getDeviceAll();
        console.log(res);
      });
      this.dialogFormVisible1 = false;
    },

    onSubmit() {
      console.log("submit!");
    },
    handleEdit(index, row) {
      this.form.devTid = row.devTid;
      this.form.name3 = row.name;
      this.form.name4 = row.amount;
      this.form.name5 = row.remark;
      console.log(index, row);
    },
    handleDelete(index, row) {
      this.delete_equitment(row.devTid);
      console.log(row.id);
      console.log(index, row);
    },
  },
  components: {},
  mounted() {
    this.getDeviceAll();
  },
  watch: {
    scId: {
      handler() {
        this.getDeviceAll();
      },
      immediate: true,
      deep: true,
    },
  },
};
</script>

<style scoped>
.el-header {
  background-color: white;

  margin: 0px 20px;
  padding: 0;
}
.visitandpiont {
  display: flex;
  margin-left: 0%;
}
.piontment {
  width: 350px;
  height: 69px;
  padding: 20px;
  border: solid 1px;
}
.underpiontment {
  width: 1600px;
  height: 60px;
  margin: 20px 0px;
  display: flex;
}
.dialog-body {
  width: 600px;
  height: 300px;
  margin-left: 100px;
  margin-top: 100px;
  margin-right: 100px;
}
.dialog-footer1 {
  position: absolute;
  left: 30%;
  bottom: 20%;
}
.dialog-footer2 {
  position: absolute;
  bottom: 20%;
  right: 30%;
}
.way {
  width: 100px;
  padding: 20px 0px;
  margin-left: -30px;
}
.drawway {
  width: 300px;
  padding: 10px 15px;
  margin-left: -30px;
}
.buttonb {
  height: 40px;
  width: 120px;
  margin-bottom: 30px;
  margin-left: 1400px;
  padding: 0px 30px 30px 25px;
  display: flex;
}
.redrawway {
  width: 300px;
  margin: 0px 10px;
  padding: 10px 0px;
}
.state {
  width: 100px;

  padding: 20px 0px;
}
.input {
  width: 300px;
  padding: 10px 0px;
  margin-left: 160px;
}
.find {
  width: 70px;
  height: 70px;
  margin: 0px 60px;
  padding: 10px 0px;
}
.carout {
  height: 63px;
  width: 292px;
  padding: 20px 30px;
  margin-left: 10px;
  display: flex;
}

.pointime {
  width: 300px;
  padding: 20px 0px;
  margin-left: -100px;
}
.drawwayblock {
  width: 280px;
  margin: 0px 30px;
  padding: 10px 15px;
}
.piontmenttime {
  width: 90px;
  padding: 20px 0px;
}
.redrawwayblock {
  width: 280px;
  margin-left: -100px;
  padding: 10px 15px;
}

.el-main {
  background-color: white;
  text-align: center;
  margin: 0px 20px;
}
</style>
